export default {
  data() {
    return {
      fileList: [],
      btnloading: false,
      index: '',
      typeArr: ['jpg', 'jpge', 'png', 'bmp', 'gif', 'zip', 'rar', 'jar', 'tar', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'pps', 'ppsx', 'txt', 'pdf'],
      typeArr1: ['jpg', 'jpge', 'png'],
      refText: '',
      type: ''
    };
  },
  methods: {
    // 单个上传
    uploading(index, type) {
      this.index = index;
      this.uploadingType = type;
    },
    beforeAvatarUpload1(file) {
      let arr = file.name.split('.');
      let type = arr[arr.length - 1];
      let isAfi = this.typeArr1.find(res => {
        return res == type;
      });
      if (!isAfi) {
        this.$message({
          message: `暂不支持上传${ type }类型文件!`,
          type: 'warning'
        });
        return false;
      } else {
        this.refText = 'upload' + this.index;
        this.$refs[this.refText][0].submit(); // 提交调用uploadFile函数
      }
    },
    uploadSectionFile1(file) {
      let fileData = new FormData(); // new formData对象
      fileData.append('formFile', file.file); // append增加数据
      this.$api.publicApi.uploadOssFile(fileData).then(res => {
        let result = res.data;
        if (result.status === 200) {
          this.$message({
            message: '上传成功',
            type: 'success'
          });
          if (this.uploadingType == 1) {
            this.form.fields[this.index].value = result.data.fileUrl;
          } else {
            let files = this.form.fields[this.index].files;
            let data = [result.data];
            if (files && files.length != 0) {
              data = [...files, result.data];
            }
            // this.form.fields[this.index].fieldItems[0].files = data;
            this.form.fields[this.index].files = data;
          }
          this.$refs[this.refText][0].clearFiles();
        } else if (result.status == 409) {
          this.$message({
            message: result.data[0].FileName + result.message,
            type: 'warning'
          });
        }
      });
    },

    // 批量上传
    uploadShow(index) {
      this.index = index;
      this.isShow = true;
    },
    beforeAvatarUpload(file) {
      let arr = file.name.split('.');
      let type = arr[arr.length - 1];
      let isAfi = this.typeArr.find(res => {
        return res == type;
      });
      if (!isAfi) {
        this.$message({
          message: `暂不支持上传${ type }类型文件!`,
          type: 'warning'
        });
      }
      return isAfi ? true : false;
    },
    uploadSectionFile(file) { // 添加文档
      let fileData = new FormData(); // new formData对象
      fileData.append('formFileData', file.file); // append增加数据
      this.btnloading = true;
      this.$api.publicApi.uploadMultipleFile(fileData).then(res => {
        let result = res.data;
        if (result.status === 200) {
          this.isShow = false;
          this.$message({
            message: '上传成功',
            type: 'success'
          });
          let files = this.form.fields[this.index].files;
          let data = [...result.data];
          if (files && files.length != 0) {
            data = [...files, ...result.data];
          }
          this.form.fields[this.index].files = data;
          this.form.fields[this.index].value = data[0].fileUrl;
          this.$refs.upload.clearFiles();
          this.fileList = [];
        } else if (result.status == 409) {
          this.$message({
            message: result.data[0].FileName + result.message,
            type: 'warning'
          });
        }
        this.btnloading = false;
      }).catch(() => {
        this.btnloading = false;
      });
    },
    addFile(file, fileList) {
      let existFile = fileList.slice(0, fileList.length - 1).find(f => f.name === file.name);
      if (existFile) {
        this.$message.error('当前文件已经存在!');
        fileList.pop();
      }
      this.fileList = fileList;
    },
    deleteFile(file, fileList) {
      this.fileList = fileList;
    },
    changeDep() { // 添加文档
      if (this.fileList.length === 0) {
        this.$message({
          message: '请先选择文件',
          type: 'warning'
        });
      } else {
        this.$refs.upload.submit(); // 提交调用uploadFile函数
      }
    },
    delFile(index, i, type) {
      this.$confirm('是否确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // if (type == 'fieldItems') {
        //   this.form.fields[index].fieldItems[0].files.splice(i, 1);
        // } else {
        this.form.fields[index].files.splice(i, 1);
        // }
        this.$message({
          message: '已删除',
          type: 'success'
        });
      });
    },
    open(url) {
      if (url.slice(0, 4) == 'http') {
        window.open(url);
      }
    }
  }
};
